package com.nchu.student_score.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nchu.student_score.dataObject.GradeResult;
import com.nchu.student_score.mapper.*;
import com.nchu.student_score.model.Appeal;
import com.nchu.student_score.model.Grade;
import com.nchu.student_score.service.AppealService;
import com.nchu.student_score.util.Define;
import com.nchu.student_score.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@Service
public class AppealServiceImpl implements AppealService {
    @Autowired
    AppealMapper appealMapper;
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    GradeTempMapper gradeTempMapper;
    @Autowired
    GradeMapper gradeMapper;
    @Autowired
    TeachTaskMapper taskMapper;
    @Autowired
    CourseMapper courseMapper;
    @Autowired
    SubScoresMapper subScoresMapper;

    @Override
    public ResultVo<Appeal> adminUpdateAppeal(Appeal input) {//管理员审批申诉
        ResultVo<Appeal> resultVo = new ResultVo<>();
        Appeal appeal = appealMapper.selectByPrimaryKey(input.getAppealId());
        if(appeal.getAppealState() == 3){
            appeal.setAdminId(input.getAdminId());//更新最新操作的管理员
            appeal.setAdminOpinion(input.getAdminOpinion());//更新管理员审核意见
            Integer state = input.getAppealState();
            if(state == 0 || state == 2 || state == 4 || state == 5){//管理员可以设置的目标申诉状态
                appeal.setAppealState(state);//更改申诉状态
                if(state == 4 && input.getNowGrade() != null){//审核通过，需要更新成绩
                    Grade grade = gradeMapper.selectByPrimaryKey(input.getGradeId());
                    grade.setScore(input.getNowGrade());
                    gradeMapper.updateByPrimaryKey(grade);
                }
                appealMapper.updateByPrimaryKey(appeal);
                resultVo.setStatus(0);
                resultVo.setMessage("申诉更新成功!");
            }
            else{
                resultVo.setStatus(2);
                resultVo.setMessage("申诉目标状态异常!");
            }
        }
        else{
            resultVo.setStatus(1);
            resultVo.setMessage("该申诉当前没有更新权限!");
        }
        return resultVo;
    }

    @Override
    public ResultVo<Appeal> teacherUpdateAppeal(Appeal input) {//教师处理申诉
        ResultVo<Appeal> resultVo = new ResultVo<>();
        Appeal appeal = appealMapper.selectByPrimaryKey(input.getAppealId());
        if(appeal.getAppealState() == 1 || appeal.getAppealState() == 2){
            appeal.setNowGrade(input.getNowGrade());//更新申诉表中的当前成绩
            appeal.setTeacherOpinion(input.getTeacherOpinion());//更新教师意见
            appeal.setAppealState(3);//更改申诉状态
            appealMapper.updateByPrimaryKey(appeal);
            resultVo.setStatus(0);
            resultVo.setMessage("申诉更新成功!");
        }
        else{
            resultVo.setStatus(1);
            resultVo.setMessage("该申诉当前没有更新权限!");
        }
        return resultVo;
    }

    @Override
    public ResultVo<GradeResult> appealGetOneGrade(Appeal input) {//教师获取一个详情
        ResultVo<GradeResult> resultVo = new ResultVo<>();

        if(input.getAppealId() == null || input.getGradeId() == null){//缺少数据
            resultVo.setStatus(1);
            return resultVo;
        }

        Grade grade = gradeMapper.selectByPrimaryKey(input.getGradeId());
        GradeResult gradeResult = new GradeResult();
        gradeResult.setTeachTask(taskMapper.selectByPrimaryKey(grade.getTeachId()));
        gradeResult.setCourse(courseMapper.selectByPrimaryKey(gradeResult.getTeachTask().getCourseId()));
        gradeResult.setStuId(grade.getStuId());
        gradeResult.setTeachId(grade.getTeachId());
        gradeResult.setScore(grade.getScore());
        gradeResult.setSubScoresList(subScoresMapper.selectByTaskIdAndStuId(gradeResult));
        gradeResult.setGradeTemp(gradeTempMapper.selectByTaskIdAndStuId(gradeResult));

        resultVo.setObj(gradeResult);
        resultVo.setStatus(0);
        return resultVo;
    }

    @Override
    public PageInfo<Appeal> getAppealList(Appeal input, Integer currPage) {//获取申诉列表
        if(currPage == null) currPage = 1;
        PageHelper.startPage(currPage, Define.PAGE_SIZE);
        PageInfo<Appeal> pageInfo = new PageInfo<>(appealMapper.selectById(input));

        List<Appeal> list = pageInfo.getList();//取出分页查询结果
        for(Appeal a : list){//将管理员的姓名加入
            if(a.getAdminId() != null)
                a.setAdminName(adminMapper.selectByPrimaryKey(a.getAdminId()).getAdminName());
        }
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public ResultVo<Appeal> stuInsertOrUpdateAppeal(Appeal input) {//学生新增/修改申诉
        ResultVo<Appeal> resultVo = new ResultVo<>();
        if(input.getGradeId() == null) return null;//没有成绩id无法申诉

        if(input.getAppealId() == null){//学生新增申诉
            SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            isoFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            input.setStartTime(isoFormat.format(new Date()));//设置当前时间作为提交时间
            appealMapper.insert(input);//插入新的申诉
            resultVo.setStatus(0);
            resultVo.setMessage("申诉提交成功!");
        }
        else{//学生更新申诉
            Appeal appeal = appealMapper.selectByPrimaryKey(input.getAppealId());
            if(appeal.getAppealState() == 0 || appeal.getAppealState() == 1){
                /*
                    <span v-if="scope.row.appealState === 0">申诉已被驳回,等待学生修改</span>
                    <span v-if="scope.row.appealState === 1">学生已提交,等待教师处理</span>
                    <span v-if="scope.row.appealState === 2">申诉已被驳回,等待教师处理</span>
                    <span v-if="scope.row.appealState === 3">教师已处理,等待管理员审核</span>
                    <span v-if="scope.row.appealState === 4">管理员审核通过,审核结束!</span>
                    <span v-if="scope.row.appealState === 5">你的申诉已经被管理员终止!</span>
                */
                appeal.setAppealContent(input.getAppealContent());//学生只能修改申诉内容
                appeal.setAppealState(1);//更改申诉状态
                appealMapper.updateByPrimaryKey(appeal);
                resultVo.setStatus(0);
                resultVo.setMessage("申诉更新成功!");
            }
            else{
                resultVo.setStatus(1);
                resultVo.setMessage("该申诉当前没有更新权限!");
            }
        }
        return resultVo;
    }
}
